class Statistics::UrlTrack
  include Mongoid::Document
  include Mongoid::Timestamps
  field :urls,     :type => Array, :default => []
  field :clicks,   :type => Array, :default => []


  # # # # # # # # # # # # # # # #
  #
  # 从这里开始是类方法
  #
  # # # # # # # # # # # # # # # #


  # # # # # # # # # # # # # # # #
  #
  # 从这里开始是对外开放的接口
  #
  # # # # # # # # # # # # # # # #

  def add_urls(_urls)
    _urls.each do |_url|
      urls   << _url unless urls.include? _urls
      clicks << []
    end
    save
  end

  def generate_url(index = 0)
    "#{Oauth2::FLOWY_URL}/ut/#{self._id}?i=#{index}"
  end

  def clicked(index)
    clicks[index] << {
      :clicked_at => Time.now
    }
    save
  end

  def all_clicked
    clicks.inject(&:+) || 0
  end

end